/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.ctu.android.DriveCommunicator.dialogs;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import cz.ctu.android.DriveCommunicator.R;
import cz.ctu.android.DriveCommunicator.activities.MainActivity;
import cz.ctu.android.DriveCommunicator.activities.MessageActivity;
import cz.ctu.android.DriveCommunicator.activities.ReplyKeyboardActivity;
import cz.ctu.android.DriveCommunicator.activities.ReplyPredefinedActivity;
import cz.ctu.android.DriveCommunicator.dialogs.ReplyMessageDialog.ButtonHandler;
import cz.ctu.android.DriveCommunicator.utils.HandlerMessage;
import cz.ctu.android.DriveCommunicator.utils.ImageMapper;
import cz.ctu.android.DriveCommunicator.utils.MessageProvider;
import cz.ctu.android.DriveCommunicator.utils.Zprava;

/**
 *
 * @author karlos
 */
public class NewMessageDialog extends Dialog{
	
	private ImageView view;
	private TextView text;
	private Zprava z;
	
 
    public NewMessageDialog(Context c) {
        super(c);
    }
    
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        // set layout
        setContentView(R.layout.dialog_message_new);
        view = (ImageView)findViewById(R.id.IncommingImage);
        text = (TextView)findViewById(R.id.IncommingName);
        simulateSMSReceived();
        
        
        Button butRead = (Button)findViewById(R.id.readSMS);
        butRead.setOnClickListener(new ButtonHandler(MessageActivity.class));
        Button cancel = (Button)findViewById(R.id.CancelRead);
        cancel.setOnClickListener(new ButtonHandler(null));
        getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    }
    
    private void simulateSMSReceived(){
    	MessageProvider msgp = MessageProvider.getInstance();
    	z = msgp.getMessage();
    	DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date dat = new Date();
    	z.setDate(dateFormat.format(dat));
    	text.setText(z.getJmeno()+" "+z.getPrijmeni());
    	view.setImageResource(ImageMapper.getImage(z.getSenderID()));
    }
    
    class ButtonHandler implements Button.OnClickListener {
        
        private Class activity;

        public ButtonHandler(Class destinationActivity) {
            activity = destinationActivity;
        }
        
        public void onClick(View view) {
        	HandlerMessage handler = new HandlerMessage();
            if (activity == MessageActivity.class) { 	
            	z.setPrectena(true);
            	try {
					handler.addMessage(z);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	Bundle bundle = new Bundle();
            	bundle.putInt("idToLoad", z.getId());
            	
                Intent intent = new Intent(getContext(), activity);
                intent.putExtras(bundle);
                getContext().startActivity(intent);
                hide();
            }else if(activity ==null){
            	z.setPrectena(false);
            	try {
					handler.addMessage(z);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	 Intent intent = new Intent(getContext(),MainActivity.class);
                 getContext().startActivity(intent);
            	 hide();
            }
        }
        
    }
    
}
